﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BLL
{
   public  class UserBll
    {
       DAL.UserDal U = new DAL.UserDal();
       public bool AddUser(Model.User user) {
           return U.AddUser(user);
       }

       public DataTable GetUserByRole(int roleId)
       {
           return U.GetUserByRole(roleId);
       }

       public DataTable GetUserAll()
       {
           return U.GetAllUser();
       }

       public bool Exists(string userId)
       {
           return U.Exists(userId);
       }

       public string GetPassword(string userId)
       {
           return U.GetPassword(userId);
       }

       public Model.User GetUser(string usrId)
       {
           DataTable table = U.GetUser(usrId);
           Model.User user = new Model.User();

           if (table.Rows.Count > 0)
           {
               if (table.Rows[0]["roleId"] != null && table.Rows[0]["roleId"].ToString() != "")
               {
                   user.roleId = int.Parse(table.Rows[0]["roleId"].ToString());
               }
               if (table.Rows[0]["roleName"] != null && table.Rows[0]["roleName"].ToString() != "")
               {
                   user.roleName = table.Rows[0]["roleName"].ToString();
               }
               if (table.Rows[0]["usrName"] != null && table.Rows[0]["usrName"].ToString() != "")
               {
                   user.usrName = table.Rows[0]["usrName"].ToString();
               }
               if (table.Rows[0]["usrPwd"] != null && table.Rows[0]["usrPwd"].ToString() != "")
               {
                   user.usrPwd = table.Rows[0]["usrPwd"].ToString();
               }
               if (table.Rows[0]["usrState"] != null && table.Rows[0]["usrState"].ToString() != "")
               {
                   user.usrState = int.Parse(table.Rows[0]["roleId"].ToString());
               }

               return user;
           }
           else
           {
               return null;
           }
       }

       public bool UpdateUserState(string userId)
       {
           return U.UpdateUserState(userId);
       }

       public int GetSateByUser(string userId)
       {
           return U.GetStateByUser(userId);
       }

       public bool UpdatePassword(string userId, string p)
       {
           return U.UpateUserPassword(userId, p);
       }
    }
}
